(function(){

    function Worker(workerPool){
        this.workerPool = workerPool;
    }
    
    Worker.prototype = {
        postMessage: function(message){
            this.workerPool.sendMessage(message, this.childWorkerId);
        },
        
        terminate: function(){
        	
        },
        
        setChildWorkerId: function(childWorkerId){
            this.childWorkerId = childWorkerId;
        },
        
        getName: function(){
            return "GearsWorker";
        },
		
		isJSONMessageSupported : function() {
			return true;
		}
    };
    
    function getPostMessageFunctionText(workerId){
        return [";function postMessage(message) {", "  google.gears.workerPool.sendMessage(message, 0);", "};"].join("\n");
    }
    
    function getOnMessageFunctionText(){
        return [";google.gears.workerPool.onmessage = function(messageText, senderId, messageObject) {", "  onmessage && onmessage({'data' : messageObject.body});", "};"].join("\n");
    }
    
    function getOnErrorFunctionText(){
        return [";google.gears.workerPool.onerror = function(error) {", "  error.name = '__WorkerError__';", "  postMessage(error);", "};"].join("\n");
    }
    
    JsWorker.GearsWorker = {
        _createWrappedWorker: function(workerPool){
		    var worker = new Worker(workerPool);
            workerPool.onmessage = function(messageText, senderId, messageObject){
                if (messageObject.body.name == "__WorkerError__") {
                    if (isFunction(onerror)) {
                        onerror.apply(worker, [messageObject.body]);
                    }
                }
                else {
                    if (isFunction(onmessage)) {
                        onmessage.apply(worker, [{
                            "data": messageObject.body
                        }]);
                    }
                }
            }
			return worker;
        },
        
        createWorkerFromUrl: function(jsUrl, onmessage, onerror){
        	if (!jsUrl) {
				throw new Error("No JavaScript URL provided!");
			}
			var workerPool = google.gears.factory.create("beta.workerpool");
			var worker = JsWorker.GearsWorker._createWrappedWorker(workerPool);
			var childWorkerId = workerPool.createWorkerFromUrl(jsUrl);
            worker.setChildWorkerId(childWorkerId);
            return worker;
        },
        
        createWorkerFromText: function(jsText, onmessage, onerror){
            if (!jsText) {
                throw new Error("No JavaScript text provided!");
            }
			var workerPool = google.gears.factory.create("beta.workerpool");        
            var worker = JsWorker.GearsWorker._createWrappedWorker(workerPool);
            var text = getPostMessageFunctionText() + jsText + getOnMessageFunctionText() + getOnErrorFunctionText();
            var childWorkerId = workerPool.createWorker(text);
            worker.setChildWorkerId(childWorkerId);
            return worker;
        }
    };
})();
